<?php

/**
* alltosun.com  index.php for newbd
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* Author: 吴松键 (wusj@alltosun.com)
* Date: 15/2/4 上午10:39
*/

class Action
{
    private $per_page = 20;

    /**
     * 显示退货列表
     */
    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);
        $refund_status = Request::getParam('refund_status', 0);
        $is_advenced   = Request::Get('is_advenced', 1);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $is_empty = 0;
        $filter = array();
        $user_filter = array();
        $goods_filter = array();
        if (isset($search_filter['refund_id']) && $search_filter['refund_id']) {
            $refund_id = order_helper::order_id_decode($search_filter['refund_id']);
            if(is_numeric($refund_id)) {
                $filter['id'] = $refund_id;
            }
        }
        if (isset($search_filter['refund_status']) && $search_filter['refund_status']) {
            $filter['refund_status'] = $search_filter['refund_status'];
        }
        if (isset($search_filter['order_id']) && $search_filter['order_id']) {
            $order_id = order_helper::order_id_decode($search_filter['order_id']);
            if(is_numeric($order_id)) {
                $filter['order_id'] = $order_id;
            }
        }
        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            $user_filter['`user_name` LIKE'] = '%'.$search_filter['user_name'].'%';
            $user_filter['status'] = 1;
            $user_ids = _model('user')->getFields('id', $user_filter);
            $filter['user_id'] = $user_ids;
        }
        if (isset($search_filter['goods_title']) && $search_filter['goods_title']) {
            //通过快照查询order_ids
            $goods_filter['`goods_title` LIKE '] = '%'.$search_filter['goods_title'].'%';
            $goods_filter['status'] = 1;
            $goods_snapshot_ids = _model('sku_snapshot')->getFields('id', $goods_filter);
            if ($goods_snapshot_ids) {
                $refund_ids = _model('refund_relation')->getFields('refund_id', array('sku_snapshot_id'=>$goods_snapshot_ids));
                $refund_ids = array_unique($refund_ids);
                $filter['id'] = $refund_ids;
            } else {
                $filter['id'] = 0;
            }
        }
        if (isset($search_filter['start_date']) && $search_filter['start_date']) {
            $filter['add_time >='] = $search_filter['start_date'].' 00:00:00';
        }
        if (isset($search_filter['end_date']) && $search_filter['end_date']) {
            $filter['add_time <='] = $search_filter['end_date'].' 23:59:59';
        }
        if (isset($refund_status) && $refund_status) {
            $filter['refund_status'] = $refund_status;
        }
        $filter['type']   = 2;
        $filter['status'] = $status;

        $refund_list = array();
        if (!$is_empty) {
            $refund_count = _model('refund')->getTotal($filter);
            if ($refund_count) {
                $pager = new Pager($this->per_page);
                if ($pager->generate($refund_count)) {
                    Response::assign('pager', $pager);
                }

                $refund_list = _model('refund')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($page));

                Response::assign('count', $refund_count);
            }
        }

        Response::assign('refund_list', $refund_list);
        Response::assign('refund_status', $refund_status);
        Response::assign('status', $status);
        Response::assign('search_filter', $search_filter);
        Response::assign('is_advenced', $is_advenced);
        Response::display('admin/barter_list.html');
    }

    /**
     * ajax 获取快递信息
     */
    public function ajax_get_express()
    {
        $express_list = _model('express')->getList(array('status'=>1));

        Response::assign('express_list', $express_list);

        Response::display('admin/express.html');
    }

    /**
     * ajax请求退货详细信息
     */
    public function ajax_refund_info()
    {
        $id = Request::getParam('id', 0);

        if (!$id) {
            return 'error';
        }

        $refund_info = service_helper::get_refund_info($id, 0);

        Response::assign('refund_info', $refund_info);
        Response::display('admin/detail.html');
    }

    /**
     * 修改信息
     */
    public function edit_refund_relation()
    {
        $relation_id = Request::getParam('relation_id', 0);
        $refund_num  = Request::getParam('refund_num', 0);
        if (!$relation_id) {
            return '请选择需要修改的退款订单';
        }

        $refund_relation_info = _uri('refund_relation', $relation_id);
        $order_relation_info  = _uri('order_relation', $refund_relation_info['order_relation_id']);
        $refund_info = _uri('refund', $refund_relation_info['refund_id']);
        if ($refund_info['refund_status'] != 1) {
            return '当前退款订单已不在可修改状态';
        }
        if ($refund_num > $order_relation_info['goods_num']) {
            $refund_num = $order_relation_info['goods_num'];
        }

        _model('refund_relation')->update(array('id'=>$relation_id), array('refund_num'=>$refund_num));

        return array(
            'info' => 'ok',
            'id'    => $refund_relation_info['refund_id']
        );
    }

    /**
     * 删除信息
     */
    public function delete_refund_relation()
    {
        $relation_id = Request::getParam('relation_id', 0);
        $status      = Request::getParam('status', 0);
        if (!$relation_id) {
            return '请选择需要删除的退款订单';
        }

        $refund_relation_info = _uri('refund_relation', $relation_id);
        $refund_info = _uri('refund', $refund_relation_info['refund_id']);
        if ($refund_info['refund_status'] != 1) {
            return '当前退款订单已不在可修改状态';
        }

        _model('refund_relation')->update(array('id'=>$relation_id), array('status'=>$status));

        return array(
            'info' => 'ok',
            'id'    => $refund_relation_info['refund_id']
        );
    }

    /**
     * 更改退货单状态
     */
    public function change_refund_status()
    {
        $id = Request::Get('id', 0);
        $refund_status = Request::Get('refund_status', 0);
        $note = Request::Get('note', '');

        if ($refund_status ==30 && !$note) {
            return '驳回理由不能为空';
        }
        if (!$refund_status) {
            return '要修改的退货状态不能为空';
        }
        $info = _uri('refund', array('id'=>$id, 'status'=>1));
        if (!$info) {
            return '该退货单不存在或被删除';
        }
        if ($info['refund_status'] == $refund_status) {
            return '要修改的状态不能和原状态一致';
        }

        $filter['refund_status'] = $refund_status;
        if ($refund_status == 5) {
            $filter['agree_time'] = date('Y-m-d H:i:s');
        }

        if ($note) {
            $filter['note'] = AnFilter::filter_string($note);
        }

        $filter['is_view'] = 0;
        $status = _model('refund')->update(array('id'=>$id), $filter);

        if ($status) {
            $reufnd_info = _uri('refund', $id);
            $email = _uri('user', $reufnd_info['user_id'], 'email');
            $goods_title = service_helper::get_goods_title($id);
            if ($refund_status == 30) {
                order_helper::sendExpressMessage('return_goods_error', array($goods_title), $reufnd_info['mobile'], $email);
            } else if ($refund_status == 5) {
                order_helper::sendExpressMessage('return_goods_success', array($goods_title), $reufnd_info['mobile'], $email);
            }
        }

        Response::redirect('barter/admin');
    }

    /**
     * 删除
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $order_info = _uri('order', (int)$v);
            if (!$order_info) {
                continue;
            }

            if ($order_info['order_status'] != 5) {
                continue;
            }

            if (1 == $order_info['status']) {
                _model('order')->update($v, array('status'=>0));
            } else {
                _model('order')->delete($v);
            }
        }

        return 'ok';
    }

    /**
     * 更改状态
     * @return array
     */
    public function change_status(){
        $id = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);

        if (!$id) {
            return '请选择要更改的内容';
        }

        $order_info = _uri('order', $id);
        if ($status == $order_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('order')->update($id, array('status'=>1));
        }

        return 'ok';
    }

    /**
     * 发货
     */
    public function send_express()
    {
        $id = Request::getParam('id', 0);
        $refund_express = Request::getParam('refund_express', '');
        $express_id = Request::getParam('express_company', '');
        if (!$id) {
            return '请选择要更改的内容';
        }
        if (!$refund_express) {
            return '请填写快递单号';
        }

        $refund_info = _uri('refund', $id);
        if (!$refund_info) {
            return '请选择要更改的内容';
        }

        if ($refund_info['refund_status'] < 5 || $refund_info['refund_status'] > 15) {
            return '当前换货单不在换货状态';
        }

        $info = array(
            'refund_status' => 15,
            'refund_express_time' => date('Y-m-d H:i:s'),
            'refund_express' => $refund_express,
            'express_id' => $express_id
        );

        $info['is_view'] = 0;
        _model('refund')->update(array('id'=>$id), $info);

        return 'ok';
    }
}
?>